//
//  MBProgressHUD+FQDExtension.m
//  FakeQDaily
//
//  Created by mewxiv on 8/20/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "MBProgressHUD+FQDExtension.h"
#import "FQDDotLoadingView.h"

static MBProgressHUD *loadingHUD;

@implementation MBProgressHUD (FQDExtension)

+ (MBProgressHUD *)showMessage:(NSString *)message
{
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:keyWindow animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.labelText = message;
    hud.labelColor = [UIColor whiteColor];
    hud.color = [UIColor blackColor];
    hud.cornerRadius = 8.0f;
    hud.margin = 8.0f;
    hud.removeFromSuperViewOnHide = YES;
    
    [hud hide:YES afterDelay:1.0f];
    
    return hud;
}

+ (MBProgressHUD *)showImage:(UIImage *)image
{
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:keyWindow animated:YES];
    hud.mode = MBProgressHUDModeCustomView;
    hud.customView = imageView;
    hud.dimBackground = NO;
    hud.color = [UIColor clearColor];
    hud.removeFromSuperViewOnHide = YES;
    [hud hide:YES afterDelay:1.0f];
    
    return hud;
    
}

+ (MBProgressHUD *)showImage:(UIImage *)image withSize:(CGSize)size
{
    return nil;
}

+ (MBProgressHUD *)showNotReachableMessage
{
    NSString *errorMsg = @"似乎已断开互联网的连接";
    return [self showMessage:errorMsg];
}

+ (MBProgressHUD *)showLoadingAnimAddedTo:(UIView *)view
{
    CGRect frame = CGRectMake(0, 0, 120, 40);
    FQDDotLoadingView *loadingView = [[FQDDotLoadingView alloc] initWithFrame:frame];
    [loadingView startLoading];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.mode = MBProgressHUDModeCustomView;
    hud.customView = loadingView;
    hud.color = [UIColor colorWithWhite:0.7f alpha:1.0f];
    hud.opacity = 0.8f;
    hud.cornerRadius = 10.0f;
    hud.margin = 20.0f;
    hud.removeFromSuperViewOnHide = NO;
    return hud;
}

+ (void)showLoadingAnim
{
    if (loadingHUD) {
        [loadingHUD show:YES];
        return;
    }
    
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    loadingHUD = [self showLoadingAnimAddedTo:keyWindow];
}

+ (void)hideLoadingAnim
{
    if (loadingHUD) {
        loadingHUD.removeFromSuperViewOnHide = YES;
        [loadingHUD hide:YES];
        loadingHUD = nil;
    }
}

@end
